//
//  PKPlayListTableViewController.m
//  PianKe
//
//  Created by lanou on 15/10/6.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "PKPlayListTableViewController.h"

#import "PKPlayerManager.h"

@interface PKPlayListTableViewController ()

@end

@implementation PKPlayListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    /**注册一个单元格*/
   
    [self.tableView registerClass:[PKPlayListTableViewCell class] forCellReuseIdentifier:@"PKPlayListTableViewCell"];
    self.tableView.rowHeight=60;
  
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(nextSongmessage:) name:@"nextSongMessage" object:@"PKPKPlayViewController"];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(beforeSongmessage:) name:@"beforeSongMessage" object:@"PKPKPlayViewController"];
}
-(void)beforeSongmessage:(NSNotification *)notification
{
    for (int i =0;i<_dataSourceArray.count;i++) {
        PKRadioDetailModel *model=_dataSourceArray[i];
        model.isSelected=NO;
        PKPlayListTableViewCell *cell=(PKPlayListTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0] ];
        cell.leftView.backgroundColor=[UIColor whiteColor];
    }
    NSInteger count=[notification.userInfo[@"plaindex"]integerValue];
    PKRadioDetailModel *model = self.dataSourceArray[count];
    model.isSelected = YES;
    [self.tableView reloadData];
}
-(void)nextSongmessage:(NSNotification *)notification
{//先遍历所有的数组，把数组中对应的model类的选中状态设置为NO,并且把cell左边的绿色小块设置为白色
    for (int i =0;i<_dataSourceArray.count;i++) {
        PKRadioDetailModel *model=_dataSourceArray[i];
        model.isSelected=NO;
               PKPlayListTableViewCell *cell=(PKPlayListTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0] ];
        cell.leftView.backgroundColor=[UIColor whiteColor];
    }

    NSInteger count=[notification.userInfo[@"plaindex"]integerValue];
    PKRadioDetailModel *model = self.dataSourceArray[count];
    model.isSelected = YES;
    [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    if (self.dataSourceArray.count==0) {
        return 1;
    }
    else
    {
    return self.dataSourceArray.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   self.cell = [tableView dequeueReusableCellWithIdentifier:@"PKPlayListTableViewCell" forIndexPath:indexPath];
    
    // Configure the cell...
#warning 当点击轮播图的时候传过来的数组是的count=0,所以不能用数组中的mdoel类来进行赋值
    if (self.dataSourceArray.count==0) {
           return self.cell;
    }
     else
     {
         PKRadioDetailModel *model =self.dataSourceArray[indexPath.row];
         self.cell.detailModel=model;
            return self.cell;
     }
 
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
#warning 跳转到当前点解的cell下标的播放位置
    [[PKPlayerManager shareManager]playWithIndex:indexPath.row];
    
    
    /**在特定的时间发送通知*/
        for (int i =0;i<_dataSourceArray.count;i++) {
        PKRadioDetailModel *model=_dataSourceArray[i];
        model.isSelected=NO;
//        if (i==indexPath.row) {
//            model.isSelected=YES;
//        }
//        else
//        {
//            model.isSelected=NO;
//        }
        PKPlayListTableViewCell *cell=(PKPlayListTableViewCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0] ];
        cell.leftView.backgroundColor=[UIColor whiteColor];
    }
    
    PKPlayListTableViewCell *cell=(PKPlayListTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    cell.leftView.backgroundColor=[UIColor greenColor];

   
    PKRadioDetailModel *model=_dataSourceArray[indexPath.row];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changSong" object:@"PKPlayListTableViewController" userInfo:[NSDictionary dictionaryWithObjectsAndKeys:indexPath,@"indexpath",model.tingid,@"tingid" ,nil]];
     

    model.isSelected=YES;
    
    

    
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
//非选中的时候
    PKPlayListTableViewCell *cell=(PKPlayListTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    cell.leftView.backgroundColor=[UIColor whiteColor];
    PKRadioDetailModel *model=_dataSourceArray[indexPath.row];
    model.isSelected=NO;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
